﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    public class StudyController : BaseController
    {
        //
        // GET: /Study/

        public ActionResult Index(byte? type, int? id)
        {
            type = type.CheckByte(0);
            ViewData["type"] = type;
            SectionInfo info = null;
            if (!id.HasValue)
            {
                info = SectionBLL.Get(null);
                if (info == null)
                    return NotFound();
                id = info.Id;
            }
            else
            {
                info = SectionBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
            }
            ViewData.Model = info;
            if (type == 0)
            {
                ViewData["content"] = info.CommonContentInfo.DetailContent;
                return View();
            }
            if(type==5)
            {
                return RedirectToAction("Internet",new{sid=id.Value});
            }
            var infoCourse = CourseStudyBLL.Get(id.Value, type.Value);
            if (infoCourse != null)
                ViewData["content"] = infoCourse.CommonContentInfo.DetailContent;
            return View();
            //return RedirectToAction("Course", new { sid = id.Value, type = type.Value });
        }

        public ActionResult Internet(int sid)
        {
            ViewData["type"] = 5;
            return View();
        }

        public ActionResult InternetListUc(int? pid,string search)
        {
            CommonRequest.ClearCache();
            var list = InternetResourcesBLL.GetList(search).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ChapterTreeUc(int id, byte type)
        {
            ViewData["type"] = type;
            var info = SectionBLL.Get(id);
            if (info == null)
                return NotFound();
            ViewData["curr"] = info;
            var chapterList = ChapterBLL.GetList().OrderBy(o => o.OrderId);
            ViewData.Model = chapterList;
            return PartialView();
        }

    }
}
